import  tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
import numpy as np

# 载入数据集
mnist =input_data.read_data_sets('MNIST_data',one_hot=True)

# 定义每个批次的大小,类似于缓冲器
batch_size =80
# 计算一共多少批次  //整除
n_batch = mnist.train.num_examples // batch_size

# 为可视化定义命名空间  名字自定义
with tf.name_scope('input'):
    # 定义PLACEHOLDER
    x = tf.placeholder(tf.float32,[None,784],name='x-input')
    y = tf.placeholder(tf.float32,[None,10],name='y-input')


# 创建简单的神经网络
# Weights = tf.Variable(tf.zeros([784,10]))
# biases = tf.Variable(tf.zeros([10]))
# stddev：正太分布的标准差
Weights1 = tf.Variable(tf.truncated_normal([784,100],stddev=0.1))
biases1 = tf.Variable(tf.zeros([100])+0.1)
# keep_prob= 百分之多少神经元正常工作 解决过拟合
L1 = tf.nn.tanh(tf.matmul(x,Weights1)+biases1)
# rate = 1- keep_prob
L1_drop = tf.nn.dropout(L1,rate=0.25)

Weights4 = tf.Variable(tf.truncated_normal([100,10],stddev=0.1))
biases4 = tf.Variable(tf.zeros([10])+0.1)

# 下面的那个交叉熵里面好像会求softmax
prediction = tf.nn.softmax(tf.matmul(L1_drop,Weights4)+biases4)
# prediction = tf.matmul(L3_drop,Weights4)+biases4

# 二次代价 分别为 均方误差最小和交叉熵平均最小
loss = tf.reduce_mean(tf.square(y-prediction))
# loss= tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y,logits=prediction))

# 梯度下降
train_step = tf.train.GradientDescentOptimizer(0.4).minimize(loss)
# 对于我的 反而没有梯度下降来的准确
# train_step = tf.train.AdamOptimizer(lr).minimize(loss)

# 初始化变量
init = tf.global_variables_initializer()

#  argmax() 返回预测值最大的是那个位置
correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(prediction,1))
# 准确率的方法  由布尔类型转化为浮点32  求均值  1.0和0
accuracy  = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))

with tf.Session() as sess:
    sess.run(init)
    # sess.graph 图的结构
    writer = tf.summary.FileWriter('logs/',sess.graph)
    # 训练21次
    for epoch in range(1):
        # 训练所有的图
        # 每次迭代 改变学习率 越靠近越慢
        for batch in range(n_batch):
            batch_xs,batch_ys = mnist.train.next_batch(batch_size)
            sess.run(train_step,feed_dict={x:batch_xs,y:batch_ys})

        test_acc = sess.run(accuracy, feed_dict={x: mnist.test.images, y: mnist.test.labels})
        train_acc = sess.run(accuracy, feed_dict={x: mnist.train.images, y: mnist.train.labels})

        print('Iter'+str(epoch)+',Testing Accuracy:'+str(test_acc)+',Training Accuracy:'+str(train_acc))


# tensorboard --logdir=logs  暂时无法使用  总是失败  暂时放下
# 总后再去考虑